include ../../Makefile.config

all: lua-src-extract lua-5.2.1 patch ../dist
	@becho "Building Lua library..."
	make -C lua-5.2.1 nspire
	@becho "Installing Lua library to the ndLua source tree..."
	make -C lua-5.2.1 install INSTALL_TOP=$(NDLUA_TOP)/extlibs/dist

../dist:
	@becho "Distribution directory doesn't exist. Creating it now."
	make -C .. dirprep

lua-src-extract:
	@becho "Extracting Lua source..."
	if [ -d lua-5.2.1 ]; then rm -rf lua-5.2.1; fi
	tar -xvf lua-5.2.1.tar.gz && touch lua-src-extract

# If we run this target, that means something made the nspireio-2.0
# source directory disappear! We need to fix that.
lua-5.2.1:
	@becho "Uh oh... the Lua source directory disappeared! Attempting to recover..."
	rm -f lua-src-extract
	make lua-src-extract
	@becho "Successfully recovered from missing Lua source directory."

patch: lua-5.2.1
	@becho "Applying patch 'lua-5.2-nspire-support.patch'..."
	cd lua-5.2.1 && patch -Ns -p0 < ../lua-5.2-nspire-support.patch && cd .. && touch patch

rmpatch: patch
	@becho "Reversing patch 'lua-5.2-nspire-support.patch'..."
	cd lua-5.2.1 && patch -Rs -p0 < ../lua-5.2-nspire-support.patch && cd .. && rm patch

clean:
	@becho "Cleaning up Lua source tree..."
	make -C lua-5.2.1 clean

distclean-init:
	@becho "Cleaning up all of Lua source tree..."

distclean: distclean-init rmpatch
	make -C lua-5.2.1 clean
	rm -rf lua-5.2.1 lua-src-extract patch

